<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Good;
use App\Cat;
use App\Comment;
use App\Cart;
use Auth;
class IndexController extends Controller
{
    //首页展示
    public function index(){
        $cat = new Cat;
        $cats = $cat->getTree();
        $best_goods = Good::where('is_best',1)->take(4)->get();
        $new_goods = Good::where('is_new',1)->take(4)->get();
        $hot_goods = Good::where('is_hot',1)->take(4)->get();
    	return view('index.index',['best_goods'=>$best_goods,'new_goods'=>$new_goods,'hot_goods'=>$hot_goods,'cats'=>$cats]);
    }

    //home个人中心/我的订单展示
    public function home(){
        $cat = new Cat;
        $cats = $cat->getTree();
    	return view('flow.myorder');
    }

    //已购买商品展示
    public function buyed(){
    	return view('flow.buyed');
    }

    /**
    *商品详情页展示
    *@param int $gid 查看的商品id
    */
    public function goods($gid){
        //评论
        $comm = Comment::where('gid',$gid)->paginate(4);
        //导航栏
        $cat = new Cat;
        $cats = $cat->getTree();
        $good = Good::find($gid);
        $cat = Cat::find($good->cat_id);
        $good->cat_name = $cat->cat_name;


        if($user = Auth::user()){
            $cart = Cart::where('uid',$user->uid)->get();
        }
    	return view('goods.goods',['good'=>$good,'cats'=>$cats,'comm'=>$comm]);
    }

    //栏目页展示

    public function cat($cid,$sort=0){
        
        $cat = new Cat;
        $cats = $cat->getTree();
        $cat = Cat::find($cid);
        $cat_name = $cat->cat_name;
        $cat_id = $cat->cat_id;
        $catg = $cat->getTree($cid);
            $ids = [];
            $ids[] = $cid;
            foreach($catg as $c){
                $ids[] = $c->cat_id;
            }
        switch($sort){
            case 0:
            $goods = Good::whereIn('cat_id',$ids)->paginate(6);
            break;
            case 1:
            $goods = Good::whereIn('cat_id',$ids)->orderBy('shop_price')->paginate(6);
            break;
            case 2:
            $goods = Good::whereIn('cat_id',$ids)->orderBy('shop_price','desc')->paginate(6);
            break;
            case 3:
            $goods = Good::whereIn('cat_id',$ids)->orderBy('click_count','desc')->paginate(6);
            break;
            case 4:
            $goods = Good::whereIn('cat_id',$ids)->where('is_hot',1)->paginate(6);
            break;
            case 5:
            $goods = Good::whereIn('cat_id',$ids)->where('is_new',1)->paginate(6);
            break;
        }
        return view('index.product',['cats'=>$cats,'goods'=>$goods,'cat_name'=>$cat_name,'cat_id'=>$cat_id]);
    }

    public function sm(Request $request,$mobile){

    

        require (base_path(). '/vendor/alidayu/TopSdk.php');
        $c = new \TopClient;
        $c->appkey = '23354613';
        $c->secretKey = '11521ed9f0bf2ebcd437863b37b6b82e';
        $rand=mt_rand(1000,9999);
        $request->session()->put('smc',$rand);
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("123456");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName("注册验证");
        $req->setSmsParam("{\"code\":\"{$rand}\",\"product\":\"shopin\"}");
        $req->setRecNum($mobile);
        $req->setSmsTemplateCode("SMS_8335325");
       
        // print_r($req);
        $resp = $c->execute($req);
        
        
        // var_dump($resp);
        
    }

    public function checksm(Request $request){
        return $request->session()->get('smc');

    }
}
